package com.nulp.psycho.web.servlet.interpretation;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nulp.psycho.db.model.Interpretation;
import com.nulp.psycho.db.model.Result;
import com.nulp.psycho.db.model.User;
import com.nulp.psycho.db.service.InterpretationService;
import com.nulp.psycho.db.service.ResultService;

@WebServlet("/AjzenkTestServlet")
public class AjzenkTestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int extraversion=0;
		int neirotizm=0;
		int lie=0;
		@SuppressWarnings("unchecked")
		List<String> ans =  (List<String>) request.getAttribute("answers");
		int testId = Integer.parseInt(request.getParameter("testIdid"));
		User user = (User) request.getSession(true).getAttribute("user");
		
		
		for (int i = 0; i < ans.size(); i++) {
			
			if((i==0)||(i==2)||(i==7)||(i==9)||(i==12)||(i==16)||(i==21)||(i==24)||(i==26)||(i==38)||(i==43)||(i==45)||(i==48)||(i==52)||(i==55)){
				if(ans.get(i).equals("Так")){
					extraversion++;
				}
			}
			if ((i==4)||(i==14)||(i==19)||(i==28)||(i==31)||(i==33)||(i==36)||(i==40)||(i==50)) {
				if(ans.get(i).equals("Ні")){
					extraversion++;
				}
			}
			if((i==1)||(i==3)||(i==6)||(i==8)||(i==10)||(i==13)||(i==15)||(i==18)||(i==20)||(i==22)||(i==25)||(i==27)||(i==30)||(i==32)||(i==34)||(i==37)||(i==39)||(i==42)||(i==44)||(i==46)||(i==49)||(i==51)||(i==54)||(i==56)){
				if(ans.get(i).equals("Так")){
					neirotizm++;
				}
			}
			if ((i==5)||(i==23)||(i==35)) {
				if(ans.get(i).equals("Так")){
					lie++;
				}
			}
			if ((i==11)||(i==17)||(i==29)||(i==41)||(i==47)||(i==53)) {
				if(ans.get(i).equals("Ні")){
					lie++;
				}
			}
		}
		
		ResultService service = new ResultService();
		Result res = new Result();
		res.setParam("екстраверсія");
		res.setValue(extraversion);
		res.setTestId(testId);
		res.setUserId(user.getId());		
		service.create(res);
		res.setParam("нейротизм");
		res.setValue(neirotizm);
		service.create(res);
		res.setParam("брехня");
		res.setValue(lie);
		service.create(res);
		
		
		System.out.println("Extra-> " + extraversion);
		System.out.println("Neiro-> " + neirotizm);
		System.out.println("Lie-> " + lie);
		System.out.println(ans);
		
		Interpretation interpretation = new Interpretation();
		interpretation = new InterpretationService().getInterpretationByValue("екстраверсія", extraversion);
		request.setAttribute("extraText", interpretation.getExplanation());
		
		interpretation = new InterpretationService().getInterpretationByValue("нейротизм", neirotizm);
		request.setAttribute("neiroText", interpretation.getExplanation());
		
		interpretation = new InterpretationService().getInterpretationByValue("брехня", lie);
		request.setAttribute("lieText", interpretation.getExplanation());
		
		request.setAttribute("extra", extraversion);
		request.setAttribute("neiro", neirotizm);
		request.setAttribute("lie", lie);
		request.getRequestDispatcher("WEB-INF/pages/results/ajzenkResult.jsp").forward(request, response);
		
		
	}

}
